﻿using System.Collections.Generic;

namespace Yz.Core.Extensions
{
    public static class DictionaryExtension
    {
        /// <summary>
        ///     尝试将键和值添加到字典中：如果不存在，才添加；存在，不添加也不抛导常
        /// </summary>
        public static Dictionary<TKey, TValue> TryAdd<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key,
            TValue value)
        {
            if (dict.ContainsKey(key) == false)
                dict.Add(key, value);
            return dict;
        }

        /// <summary>
        ///     将键和值添加或替换到字典中：如果不存在，则添加；存在，则替换
        /// </summary>
        public static Dictionary<TKey, TValue> AddOrPeplace<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key,
            TValue value)
        {
            dict[key] = value;
            return dict;
        }

        /// <summary>
        ///     获取与指定的键相关联的值，如果没有则返回输入的默认值
        /// </summary>
        public static TValue GetValue<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue defaultValue)
        {
            return dict.ContainsKey(key) ? dict[key] : defaultValue;
        }

        /// <summary>
        ///     向字典中批量添加键值对
        /// </summary>
        /// <param name="replaceExisted">如果已存在，是否替换</param>
        public static Dictionary<TKey, TValue> AddRange<TKey, TValue>(this Dictionary<TKey, TValue> dict,
            IEnumerable<KeyValuePair<TKey, TValue>> values, bool replaceExisted)
        {
            foreach (var item in values)
                if (dict.ContainsKey(item.Key) == false || replaceExisted)
                    dict[item.Key] = item.Value;
            return dict;
        }
        /// <summary>
        /// 追加字典的值列表
        /// </summary>
        /// <typeparam name="K">key类型</typeparam>
        /// <typeparam name="V">value的类型</typeparam>
        /// <param name="dics">字典集合</param>
        /// <param name="theKey">key值</param>
        /// <param name="theValue">value值</param>
        public static void AddVerify<K, V>(this Dictionary<K, List<V>> dics, K theKey, V theValue)
        {
            if (dics.ContainsKey(theKey))
            {
                dics[theKey].Add(theValue);
            }
            else
            {
                dics.Add(theKey, new List<V> { theValue });
            }
        }
    }
}
